<?php
declare (strict_types=1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class ClassTime extends Model
{
    protected $table = 'classtime';

    public function getList($schoolId, $timeDate, $name, $teacherName)
    {
        return $this->alias('cs')->with('classStudent')
                    ->join('student t', 't.id=cs.student_id')
                    ->filed('cs.*,t.name student_name,t.sex,t.mobile,t.birthday');
    }

    public function room()
    {
        return $this->hasOne(Classroom::class,'id','classroom_id')->bind(['room_name'=>'name']);
    }

    public function teacher()
    {
        return $this->hasOne(Admins::class,'id','admin_id')->bind(['ename']);
    }

    public function course()
    {
        return $this->hasOne(Course::class,'id','course_id')->bind(['course_name'=>'name']);
    }

    public function getData1($schoolId = null, $time = null, $name = null, $teacherName = null)
    {
//        return $this->hasMany(ClassStudent::class, 'classtime_id', 'id')
//            ->alias('cs')
//            ->join('student t', 't.id=cs.student_id')
//            ->join('classtime ct','ct.id=cs.classtime_id')
//            ->join('admins a','a.id=ct.admin_id')
//            ->join('invoice_details ivd', 'cs.invoice_details_id=ivd.id')
//            ->join('invoice i', 'i.id=ivd.invoice_id' )
//            ->where('cs.type', 1)
//            ->where('ivd.status', 1)
//            ->where('t.status', '<>', -1)
//            ->where('i.status', 1)
//            ->order('ct.start_time')
//            ->field('class_student.*,t.name student_name,t.sex,t.mobile,t.birthday,ivd.name course_name,a.name teacher_name')
//            ->select()
//            ->toArray();

//        return $this->with('ClassStudentDataInfo')->select();


        return $this->with(['ClassStudentDataInfo' => function ($query) use ($name, $schoolId) {
            $query->join('student s', 'class_student.student_id=s.id')
                ->join('invoice_details ivd', 'class_student.invoice_details_id=ivd.id')
                ->join('invoice i', 'ivd.invoice_id=i.id')
                ->where(function ($q) use ($name, $schoolId) {
                    $q->where('i.school_id', $schoolId)
                        ->whereRaw("(concat(IFNULL(s.name,''),IFNULL(s.ename,''),IFNULL(s.card_number,'')) like '%" . $name . "%')");
                })
                ->where('class_student.type', 1)
                ->where('class_student.state', '<>', '取消')
                ->where('ivd.status', 1)
                ->where('s.status', '<>', -1)
                ->where('i.status', 1);
        }])
            ->join('admins a', 'a.id=classtime.admin_id')
            ->join('classroom cr', 'cr.id=classtime.classroom_id')
            ->join('course c', 'c.id=classtime.course_id')
////            ->where('classhours.duration', '>', 0)
            ->where(function ($query) use ($time) {
                if ($time != null) {
                    $query->whereBetween('classtime.start_time', [$time['start_time'], $time['end_time']]);
                }
            })
            ->where(function ($query) use ($teacherName) {
                if ($teacherName != null) {
                    $query->whereRaw("(concat(IFNULL(a.username,''),IFNULL(a.name,''),IFNULL(a.ename,''),IFNULL(a.number,'')) like '%" . $teacherName . "%')");
                }
            })
            ->field('classtime.id,classtime.admin_id,classtime.remark,classtime.classroom_id,classtime.course_id,classtime.start_time,classtime.end_time,classtime.duration,classtime.NumberPeople,a.name as teacher_name,cr.name as roomname,cr.number,c.name as course_name')
//            ->field('classhours.id,classhours.admin_id,classhours.remarks,classhours.classroom_id,classhours.courses_id,classhours.start_time,classhours.end_time,classhours.duration,classhours.NumberPeople,a.z_name as teacher_name,cr.name as classroomName,cr.number,c.name as courses_name')
            ->order('classtime.start_time')
            ->select()
            ->toArray();

//        return $this->with(['ClassStudentDataInfo' => function ($query) use ($name, $schoolId) {
//            $query->join('student s', 'class_student.student_id=s.id')
//                ->join('invoice_details ivd', 'class_student.invoice_details_id=ivd.id')
//                ->join('invoice i', 'ivd.invoice_id=i.id')
//                ->where(function ($q) use ($name, $schoolId) {
//                    $q->where('i.school_id', $schoolId)
//                        ->whereRaw("(concat(IFNULL(s.name,''),IFNULL(s.ename,''),IFNULL(s.card_number,'')) like '%" . $name . "%')");
//                })
//                ->where('class_student.type', 1)
//                ->where('class_student.state', '<>', '取消')
//                ->where('ivd.status', 1)
//                ->where('s.status', '<>', -1)
//                ->where('i.status', 1);
//        }])
//            ->join('admins a', 'a.id=classtime.admin_id')
//            ->join('classroom cr', 'cr.id=classtime.classroom_id')
//            ->join('course c', 'c.id=classtime.course_id')
//////            ->where('classhours.duration', '>', 0)
//            ->where(function ($query) use ($time) {
//                if ($time != null) {
//                    $query->whereBetween('classtime.start_time', [$time['start_time'], $time['end_time']]);
//                }
//            })
//            ->where(function ($query) use ($teacherName) {
//                if ($teacherName != null) {
//                    $query->whereRaw("(concat(IFNULL(a.username,''),IFNULL(a.name,''),IFNULL(a.ename,''),IFNULL(a.number,'')) like '%" . $teacherName . "%')");
//                }
//            })
//            ->field('classtime.id,classtime.admin_id,classtime.remark,classtime.classroom_id,classtime.course_id,classtime.start_time,classtime.end_time,classtime.duration,classtime.NumberPeople,a.name as teacher_name,cr.name as roomname,cr.number,c.name as course_name')
////            ->field('classhours.id,classhours.admin_id,classhours.remarks,classhours.classroom_id,classhours.courses_id,classhours.start_time,classhours.end_time,classhours.duration,classhours.NumberPeople,a.z_name as teacher_name,cr.name as classroomName,cr.number,c.name as courses_name')
//            ->order('classtime.start_time')
//            ->select()
//            ->toArray();

    }

    public function ClassStudentDataInfo()
    {
        return $this->hasMany(ClassStudent::class, 'classtime_id', 'id')
            ->join('student', 'class_student.student_id=student.id')
            ->join('invoice_details', 'class_student.invoice_details_id=invoice_details.id')
            ->join('invoice', 'invoice_details.invoice_id=invoice.id')
            ->where('class_student.type', 1)
            ->where('invoice_details.status', 1)
            ->where('student.status', '<>', -1)
            ->where('invoice.status', 1)
            ->field('class_student.*,student.name,student.sex,student.mobile,student.birthday,student.education,invoice_details.name as courseName');
            ;
    }

    public function classStudent()
    {
        return $this->belongsTo(ClassStudent::class, 'classtime_id', 'id');
    }
}
